package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CategoryShieldPartsRelationMapper;
import com.ztgf.commodity.models.bo.CategoryShieldPartsRelationBO;
import com.ztgf.commodity.models.bo.CategoryShieldPartsRelationSaveBO;
import com.ztgf.commodity.models.dto.CategoryShieldPartsRelationDTO;
import com.ztgf.commodity.models.dto.CommodityCategoryDTO;
import com.ztgf.commodity.models.service.ICategoryShieldPartsRelationService;
import com.ztgf.commodity.models.service.ICommodityCategoryService;
import com.ztgf.commodity.models.vo.CategoryShieldPartsRelationVO;
import com.ztgf.commodity.models.vo.ShieldPartsParentVO;
import com.ztgf.commodity.models.vo.ShieldPartsVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类盾构部位关系表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-02 15:12:13
 */
@DubboService
public class CategoryShieldPartsRelationServiceImpl extends ServiceImpl<CategoryShieldPartsRelationMapper, CategoryShieldPartsRelationDTO> implements ICategoryShieldPartsRelationService {

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private ICommodityCategoryService categoryService;

    @Override
    public boolean add(CategoryShieldPartsRelationSaveBO saveBO) {
        //查询类目，只有最顶级类目才能关联
        CommodityCategoryDTO categoryDTO = categoryService.selectById(saveBO.getCategoryId());
        if (categoryDTO != null) {
            if (categoryDTO.getParentId() == 0) {
                CategoryShieldPartsRelationDTO dto = new CategoryShieldPartsRelationDTO();
                BeanUtils.copyProperties(saveBO, dto);
                Long id = ymlConfig.getPkId();
                dto.setId(id);
                return this.insert(dto);
            }
        }
        return false;
    }

    @Override
    public Page<CategoryShieldPartsRelationVO> queryPageList(CategoryShieldPartsRelationBO bo) {
        Page<CategoryShieldPartsRelationVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<CategoryShieldPartsRelationVO> list = baseMapper.queryPageList(page, bo);
        page.setRecords(list);
        return page;
    }

    @Override
    public ShieldPartsParentVO queryShieldParts(Long id) {
        ShieldPartsParentVO vo = new ShieldPartsParentVO();
        List<ShieldPartsVO> list = baseMapper.queryShieldParts(id);
        vo.setName("适用盾构部位");
        vo.setList(list);
        return vo;
    }

    @Override
    public boolean deleteAccordingToCategory(Long categoryId) {
        Wrapper<CategoryShieldPartsRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        List<CategoryShieldPartsRelationDTO> list = this.selectList(wrapper);
        if (list != null && list.size() > 0) {
            return this.deleteBatchIds(list);
        }
        return false;
    }
}
